package com.viewpagertext.DBHelper.Helper;

import com.viewpagertext.DBHelper.AppDatabase;
import com.viewpagertext.DBHelper.Dao.RecentPlayDao;
import com.viewpagertext.Model.RecentPlay;

public class RecentPlayHelper {
    private RecentPlayDao recentPlayDao;
    private static final int MAX_RECORDS = 50;

    public RecentPlayHelper(){
        recentPlayDao = AppDatabase.getInstance().recentPlayDao();
    }
    public void insertRecentPlayEntity(RecentPlay recentPlay){
        // 重复记录删除较早的记录
        if (recentPlayDao.getRecentPlayByNameAndSinger(recentPlay.name, recentPlay.singer) != null){
            recentPlayDao.deleteRecentPlayByNameAndSinger(recentPlay.name, recentPlay.singer);
        }
        // 超过50条则删除最前面的数据
        int count = recentPlayDao.getRecordCount();
        if (count >= MAX_RECORDS) recentPlayDao.deleteOldestIfLimitExceeded();
        recentPlayDao.insertRecentPlay(recentPlay);
    }
}
